import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/reset.css'
import axios from 'axios'

// 配置Vue Feature flags
window.__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ = false
window.__VUE_OPTIONS_API__ = true
window.__VUE_PROD_DEVTOOLS__ = false

// 配置axios
axios.defaults.baseURL = ''  // 使用相对路径，通过代理转发请求
axios.defaults.timeout = 10000

// 请求拦截器
axios.interceptors.request.use(
  config => {
    const token = localStorage.getItem('admin_token')
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    if (error.response) {
      const status = error.response.status
      if (status === 401) {
        // 未授权，清除token并跳转到登录页
        localStorage.removeItem('admin_token')
        localStorage.removeItem('admin_user_info')
        router.push('/login')
      }
    }
    return Promise.reject(error)
  }
)

const app = createApp(App)
const pinia = createPinia()

app.use(router)
app.use(pinia)
app.use(Antd)
app.mount('#app')
